/**
 * 显示当前信息的布告板
 */
public class CurrentConditionDisplay implements Observer, DisplayElement {

    private IObservable iObservable;
    private float temperature;
    private float humidity;

    public CurrentConditionDisplay() { }

    public CurrentConditionDisplay(IObservable iObservable) {
        this.iObservable = iObservable;
        iObservable.registerObserver(this);
    }

    public void setiObservable(IObservable iObservable) {
        this.iObservable = iObservable;
    }

    @Override
    public void display() {
        System.out.println("temperature: " + temperature + ", humidity: " + humidity);
    }

    @Override
    public void update(IObservable iob, Object arg) {
        if (arg == null) {  // 只能自己拉数据
            if (iob instanceof WeatherData) {
                WeatherData weatherData = (WeatherData) iob;
                this.temperature = weatherData.getTemperature();
                this.humidity = weatherData.getHumidity();
            }
        } else {    // 主题有推送数据
            /* 可以自己拉或者用主题的推送数据 */
            Data data = (Data) arg;
            this.temperature = data.getTemperature();
            this.humidity = data.getHumidity();
        }
    }
}
